package com.littleant.controller;

import java.util.*;
import com.google.gson.*;
import com.google.gson.JsonArray;
import com.littleant.model.ConfigurationDataEntity;
import com.littleant.repository.DataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2016/11/3.
 */
@Controller
public class MainController {
    @Autowired
    DataRepository dataRepository;



    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String test(Map<String,Object> map, Model model, ModelMap modelMap){
        return  "index";
    }
    @RequestMapping(value = "/getListData", method = RequestMethod.POST)
    @ResponseBody
    public String getChildrenMenu (ModelMap modelMap){
        List<ConfigurationDataEntity> dataList = dataRepository.findAll();
       // ConfigurationDataEntity parent_data =  dataRepository.findOne(Integer.valueOf(dataId));
        Gson gson = new Gson();
        String jsonstring2 = gson.toJson(dataList);
        return  jsonstring2.toString();
    }

    @RequestMapping(value = "/GetData", method = RequestMethod.GET)
    @ResponseBody
    public  String getHomeMenu(ModelMap modelMap) throws Exception {
        // 查询表中所有记录
        JsonArray array = new JsonArray();
        List<ConfigurationDataEntity> dataList = dataRepository.findPart();
        System.out.println("dataList是否为空"+dataList.isEmpty());
        //System.out.println(dataList.get(0).getDataId());
        // 将所有记录传递给要返回的jsp页面，放在dataList当中
        // modelMap.addAttribute("dataList", dataList);
        Gson gson = new Gson();

        String jsonstring = gson.toJson(dataList);
        System.out.println("jsonstring 为"+jsonstring);
        return jsonstring;
    }

    @RequestMapping(value = "/dataDelete", method = RequestMethod.POST)
    @ResponseBody
    //将id传递到里面
    public String deleteData(@RequestParam(value="id") String dataId) {

        // String s = RequestParam("id");
        System.out.println("开始删除咯");
        System.out.println("dataID为"+dataId);
        // 删除id为dataId的用户
        dataRepository.delete(Integer.valueOf(dataId));

        // 立即刷新
        dataRepository.flush();
        return "success";
    }

    @RequestMapping(value = "/dataListDelete", method = RequestMethod.POST)
    @ResponseBody
    public String dataListDelete(@RequestParam(value="id") String dataId) {
        System.out.println("要删除的dataId是"+dataId);
        // 删除id为dataId的用户
        dataRepository.delete(Integer.valueOf(dataId));
        // 立即刷新
        dataRepository.flush();
        System.out.println("datalist删除成功");
        return null;
    }


    // post请求，处理添加数据请求，并重定向到index
    @RequestMapping(value = "/addData", method = RequestMethod.POST)
    @ResponseBody
    public String addData(@RequestParam(value="dataName") String dataName,@RequestParam(value="dataPid") String dataPid) {
        String s=addNewNode(dataPid,dataName,"2");
        return s;
    }

    @RequestMapping(value = "/addListData", method = RequestMethod.POST)
    @ResponseBody
    public String addListData(@RequestParam(value="dataPid") String dataPid,@RequestParam(value="dataName") String dataName,@RequestParam(value="dataType") String dataType) {
        String s=addNewNode(dataPid,dataName,dataType);
        return s;
    }

    public String addNewNode(String dataPid,String dataName,String dataType){
        ConfigurationDataEntity newlistdata = new ConfigurationDataEntity();
        newlistdata.setDataPid(Integer.parseInt(dataPid));
        newlistdata.setDataName(dataName);
        newlistdata.setDataType(dataType);
        //newlistdata.setDataId(addauto_id);
        //newlistdata.setFullname(getFullName(100087));
        System.out.println("添加到listdata");
        // 数据库中添加一组数据，并立即刷新缓存.
        // save()和saveAndFlush()：save()方法处理完毕后，数据依然在缓冲区未写入数据库
        // 使用saveAndFlush()可以立即刷新缓冲区，写入数据库
        dataRepository.saveAndFlush(newlistdata);
        System.out.println(dataRepository.findMaxId());
        newlistdata.setFullname(getFullName(dataRepository.findMaxId()));
        dataRepository.saveAndFlush(newlistdata);

        if (dataType.equals("1")){
            newlistdata.setDataType("Configuration");
        }
        if (dataType.equals("2")||dataType.equals("section")||dataType.equals("Section")){
            newlistdata.setDataType("Section");
        }
        if (dataType.equals("3")||dataType.equals("property")||dataType.equals("Property")){
            newlistdata.setDataType("Property");
        }
        if (dataType.equals("4")||dataType.equals("value")||dataType.equals("Value")){
            newlistdata.setDataType("Value");
        }

        Gson gson = new Gson();
        String jsonstring4 = gson.toJson(newlistdata);
        return jsonstring4;
    }


    @RequestMapping(value = "/selectedNode", method = RequestMethod.POST)
    @ResponseBody
    public  String selectedNode(@RequestParam(value="name") String dataName){
        System.out.println(dataName);
        int dataId=Integer.parseInt(dataName);
        List<ConfigurationDataEntity> selectedList=dataRepository.findByName3(dataId);
        for (ConfigurationDataEntity data:selectedList
                ) {
            if (data.getDataType().equals("1")){
                data.setDataType("Configuration");
            }
            if (data.getDataType().equals("2")||data.getDataType().equals("section")||data.getDataType().equals("Section")){
                data.setDataType("Section");
            }
            if (data.getDataType().equals("3")||data.getDataType().equals("property")||data.getDataType().equals("Property")){
                data.setDataType("Property");
            }
            if (data.getDataType().equals("4")||data.getDataType().equals("value")||data.getDataType().equals("Value")){
                data.setDataType("Value");
            }
        }
        System.out.println(selectedList.size());
        Gson gson = new Gson();
        String jsonstring3 = gson.toJson(selectedList);
        System.out.println(jsonstring3.toString());
        return  jsonstring3.toString();


    }


    @RequestMapping(value = "/dataEdit", method = RequestMethod.POST)
    @ResponseBody
    public String dataEdit(@RequestParam(value="dataId") String dataId,@RequestParam(value="dataPid") String dataPid,@RequestParam(value="dataName") String dataName,@RequestParam(value="dataType") String dataType,@RequestParam(value="fullname") String fullname) {
        System.out.println("要修改的dataId是"+dataId);
        // 删除id为dataId的用户
        ConfigurationDataEntity editdata = dataRepository.findOne(Integer.parseInt(dataId));
        editdata.setDataName(dataName);

        if (dataType.equals("Configuration")){
            editdata.setDataType("1");
        }
        if (dataType.equals("section")||dataType.equals("Section")){
            editdata.setDataType("2");
        }
        if (dataType.equals("property")||dataType.equals("Property")){
            editdata.setDataType("3");
        }
        if (dataType.equals("value")||dataType.equals("Value")){
            editdata.setDataType("4");
        }
        dataRepository.saveAndFlush(editdata);
        editdata.setFullname(getFullName(Integer.parseInt(dataId)));

        // 立即刷新
        dataRepository.saveAndFlush(editdata);
        editdata.setDataType(dataType);
        System.out.println("datalist修改成功");
        System.out.println(editdata.getFullname());
        Gson gson = new Gson();
        String editresult = gson.toJson(editdata);
        return  editresult.toString();
    }

    public String getFullName(int i){
        String fullname="";
        ConfigurationDataEntity config=new ConfigurationDataEntity();
        config=dataRepository.findOne(i);
        fullname=config.getDataName();
        System.out.println(fullname);
        if(config.getDataPid()==null)
            return fullname;
        else
            return getFullName(config.getDataPid())+"."+fullname;
    }

}
